﻿using UnityEngine;
using System.Collections;

public class EnemyFollow : MonoBehaviour {

	public Transform target;
	public float followDistance = 5;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void FixedUpdate () 
	{
		if (target==null) return;
		Vector3 dist = target.position - transform.position;

		if (dist.magnitude>followDistance)
		{
			transform.position += dist*Time.deltaTime;
			transform.position = new Vector3 (transform.position.x, transform.position.y, 0);
		}

		rigidbody.velocity = Vector3.Lerp (rigidbody.velocity, Vector3.zero, Time.deltaTime);

	}
}
